package com.skcc.cop.mrtaxi4driver.http;

import java.net.SocketTimeoutException;

import android.content.Context;
import android.util.Log;

import com.projectteam.http.api.BaseHttpApi;
import com.projectteam.http.exception.NoNetworkException;
import com.projectteam.http.exception.NoResponseException;
import com.projectteam.http.util.DeviceUtil;
import com.skcc.cop.mrtaxi4driver.http.json.AcceptCustomerReq;
import com.skcc.cop.mrtaxi4driver.http.json.AcceptCustomerRes;
import com.skcc.cop.mrtaxi4driver.http.json.CommonHeader;
import com.skcc.cop.mrtaxi4driver.http.json.GetCallListReq;
import com.skcc.cop.mrtaxi4driver.http.json.GetCallListRes;
import com.skcc.cop.mrtaxi4driver.http.json.RegisterDriverPosReq;
import com.skcc.cop.mrtaxi4driver.http.json.RegisterDriverPosRes;
import com.skcc.cop.mrtaxi4driver.http.json.RegisterPushIdReq;
import com.skcc.cop.mrtaxi4driver.http.json.RegisterPushIdRes;
import com.skcc.cop.mrtaxi4driver.http.model.Result;
import com.skcc.cop.mrtaxi4driver.util.Utils;

public class BizGateway extends BaseHttpApi {
	private final static String TAG = "BizGateway";
	
	private Context context;
	
	
	public BizGateway(Context context, String serverUrl) {
		super(context, serverUrl);
		
		this.context = context;
	}
	
	
	private CommonHeader getCommonHeader() {
	    	
		DeviceUtil device = new DeviceUtil(context);
		
    	CommonHeader commonHeader = new CommonHeader();
    	commonHeader.setTransactionId(device.getIMEI() + "_" + Utils.getFormattedDate());
    	commonHeader.setImei(device.getIMEI());
    	//commonHeader.setMsisdn(device.getMDN());
    	commonHeader.setMsisdn("01064000000");
    	commonHeader.setAppVersion(device.getAppVersion());
    	commonHeader.setOsModel(device.getDeviceModel());
    	commonHeader.setOsVersion(device.getOsVersion());
    	
    	return commonHeader;
    }
	
	
	/**
	 * registration id 을 서버로 전송하는 메소드
	 * @param pushId 단말의 registrationId
	 * @param listener
	 */
	public void registerPushId(final String pushId, final RegisterPushIdListener listener) {
		new Thread(new Runnable() {
			public void run() {
				
				RegisterPushIdReq req = new RegisterPushIdReq();
				req.setCommonHeader(getCommonHeader());
				req.setPushId(pushId);
				final RegisterPushIdRes res;
				
				try {
					res = (RegisterPushIdRes) getResponse(req, RegisterPushIdRes.class);
				} catch (NoNetworkException nne) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoNetwork(); 
						}
					});
					return;
				} catch (NoResponseException nre) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				} catch (SocketTimeoutException ste) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onSessionTimeOut();
						}
					});
					return;
				} catch (Exception e) {
					e.printStackTrace();
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				}
				
				Utils.makeLooperThread(new Runnable() {
					public void run(){
				
						if (res == null) {
							listener.onNoResponse();
							return;
						}
						
						if (res.getResult().getCode() != Result.OK) {
							listener.onFail(res.getResult().getCode(), res.getResult().getMessage());
						} else {
							listener.onSuccess();
						}
						
					}
				});
			}
		}).start();
		
	}
	

	/**
	 * Call 내역 조회
	 * @param deviceId
	 * @param listener
	 */
	public void getCallList(final String acceptYn, final GetCallListListener listener) {
		new Thread(new Runnable() {
			public void run() {
				
				GetCallListReq req = new GetCallListReq();
				req.setCommonHeader(getCommonHeader());
				req.setAcceptYn(acceptYn);
				final GetCallListRes res;
				
				try {
					res = (GetCallListRes) getResponse(req, GetCallListRes.class);
				} catch (NoNetworkException nne) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoNetwork(); 
						}
					});
					return;
				} catch (NoResponseException nre) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				} catch (SocketTimeoutException ste) {
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onSessionTimeOut();
						}
					});
					return;
				} catch (Exception e) {
					e.printStackTrace();
					Utils.makeLooperThread(new Runnable() {
						public void run() {
							listener.onNoResponse();
						}
					});
					return;
				}
				
				Utils.makeLooperThread(new Runnable() {
					public void run(){
				
						if (res == null) {
							listener.onNoResponse();
							return;
						}
						
						if (res.getResult().getCode() != Result.OK) {
							listener.onFail(res.getResult().getCode(), res.getResult().getMessage());
						} else {
							listener.onSuccess(res.getCallList());
						}
						
					}
				});
			}
		}).start();
		
	}
	public void acceptCustomerReq(final String callId, final String acceptYn) {
		new Thread(new Runnable() {
			public void run() {
				
				AcceptCustomerReq req = new AcceptCustomerReq();
				req.setCommonHeader(getCommonHeader());
				req.setCallId(callId);
				req.setAcceptYn(acceptYn);
				final AcceptCustomerRes res;
				
				try {
					res = (AcceptCustomerRes) getResponse(req, AcceptCustomerRes.class);
				} catch (NoNetworkException nne) {
					return;
				} catch (NoResponseException nre) {
					return;
				} catch (SocketTimeoutException ste) {
					return;
				} catch (Exception e) {
					e.printStackTrace();
					return;
				}
				
				Utils.makeLooperThread(new Runnable() {
					public void run(){
				
						if (res == null) {
							return;
						}
						
						if (res.getResult().getCode() != Result.OK) {
							Log.i("acceptCustomerReq >>> ",
										"getCode: " + res.getResult().getCode() + ", " +
										"getMessage: " + res.getResult().getMessage() );
						} else {
							Log.i("acceptCustomerReq >>> ", "success!!");  
						}
						
					}
				});
			}
		}).start();
	}
	
	
	public void registerDriverPos(final double latitude, final double longitude) {
		new Thread(new Runnable() {
			public void run() {
				
				RegisterDriverPosReq req = new RegisterDriverPosReq();
				req.setCommonHeader(getCommonHeader());
				req.setLatitude(latitude);
				req.setLongitude(longitude);
				final RegisterDriverPosRes res;
				
				try {
					res = (RegisterDriverPosRes) getResponse(req, RegisterDriverPosRes.class);
				} catch (NoNetworkException nne) {
					return;
				} catch (NoResponseException nre) {
					return;
				} catch (SocketTimeoutException ste) {
					return;
				} catch (Exception e) {
					e.printStackTrace();
					return;
				}
				
				Utils.makeLooperThread(new Runnable() {
					public void run(){
				
						if (res == null) {
							return;
						}
						
						if (res.getResult().getCode() != Result.OK) {
							Log.i("RegisterDriverPos >>> ",
										"getCode: " + res.getResult().getCode() + ", " +
										"getMessage: " + res.getResult().getMessage() );
						} else {
							Log.i("RegisterDriverPos >>> ", "success!!"); 
						}
						
					}
				});
			}
		}).start();
	}
	
}
